﻿using BaiduBce;
using BaiduBce.Auth;
using BaiduBce.Services.Bos;
using BaiduBce.Services.Bos.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace DefeatEpidemic.Common
{
    public class FIleUpload
    {            
        /// <summary>
        /// 通过文件流上传文件至百度bos
        /// </summary>
        /// <param name="byteData">文件字节数组</param>
        /// <param name="fileName">文件名</param>
        /// <returns>上传成功返回JSON字符串</returns>
        public static string FileSaveAs(byte[] byteData, string fileName)
        {
            try
            {                
                //检查文件字节数组是否为NULL
                if (byteData == null)
                {
                    return "文件不可为空";
                }
                BosClient client = GenerateBosClient();
                string dir_name = "virus";
                PutObjectResponse putObjectFromFileResponse = client.PutObject(dir_name, fileName, byteData);
                return $"https://defeat.cdn.bcebos.com/{dir_name}/{fileName}";
                
            }
            catch(Exception ex)
            {
                return $"上传过程中发生意外错误{ex.Message}";
            }
        }

        public static BosClient GenerateBosClient()
        {
            const string accessKeyId = "59322735505f416f9dd1d4b051d13cab"; // 您的Access Key ID
            const string secretAccessKey = "6e032c9cae6d40e1aca988b686f64d5e"; // 您的Secret Access Key            
            const string endpoint = "https://defeat.bd.bcebos.com";          //传入Bucket所在区域域名

            // 初始化一个BosClient
            BceClientConfiguration config = new BceClientConfiguration();
            config.Credentials = new DefaultBceCredentials(accessKeyId, secretAccessKey);
            config.Endpoint = endpoint;
            BosClient client = new BosClient(config);
            return client;
        }
    }
}
